package com.dongqi.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongqi.myblog.po.TBlogTag;
import com.dongqi.myblog.po.TTag;
import com.dongqi.myblog.mapper.TTagMapper;
import com.dongqi.myblog.po.vo.TagQuery;
import com.dongqi.myblog.service.TBlogTagService;
import com.dongqi.myblog.service.TTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dongqi
 * @since 2020-08-03
 */
@Service
public class TTagServiceImpl extends ServiceImpl<TTagMapper, TTag> implements TTagService {

    @Autowired
    private TBlogTagService blogTagService;

    @Override
    public List<TagQuery> getTagIndex(Integer pageSize) {
        return this.baseMapper.selectTagList(pageSize);
    }

    /**
     * 后台功能：查找所有的tag
     */
    @Override
    public Page<TTag> pageTags(Page<TTag> page) {
        QueryWrapper<TTag> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        this.baseMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 保存标签
     */
    @Override
    @Transactional
    public Boolean saveTag(TTag tag) {
        tag.setName(tag.getName().trim());
        int insert = this.baseMapper.insert(tag);
        if (insert>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除标签
     */
    @Override
    @Transactional
    public Boolean deleteById(Long id) {
        QueryWrapper<TBlogTag> wrapper = new QueryWrapper<>();
        wrapper.eq("tag_id",id);
        blogTagService.remove(wrapper);
        return this.removeById(id);
    }

    /**
     * 更新tag
     * @param tag
     * @return
     */
    @Override
    public Boolean updateTag(TTag tag) {
        return this.updateById(tag);
    }

}
